package com.lwq.cocos;

import java.awt.Graphics;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.annotations.Expose;
import com.lwq.util.Constance;
import com.lwq.util.FormulaUtil;
import com.lwq.util.JsUtil;
import com.lwq.view.CenterEditorView;

public class CCNode extends CCObject {

	@Expose
	private List<CCNode> Childs = null;
	@Expose
	private String PosX = "0";
	@Expose
	private String PosY = "0";
	@Expose
	private String ScaleX = "1";
	@Expose
	private String ScaleY = "1";
	@Expose
	private String Anchor = Constance.ANCHOR_CENTER_NAME;
	@Expose
	private String Rotate = "0";
	@Expose
	private String Visible = Constance.TRUE_STR;
	@Expose
	private String Expct = null;
	@Expose
	public Map<String, String> ExpMap = null;

	public float mPosX = 0;
	public float mPosY = 0;
	public float mScaleX = 1;
	public float mScaleY = 1;
	public int mAnchor = Constance.ANCHOR_CENTER;
	public Double mRotate = 0.0;
	public boolean mVisible = true;

	public CCNode(String name) {
		super(name);
		setType(Constance.NODE_TYPE);
		Childs = new ArrayList<CCNode>();
	}

	public String getAnchor() {
		return Anchor;
	}

	public void setAnchor(String anchor) {
		Anchor = anchor;
	}

	public String getRotate() {
		return Rotate;
	}

	public void setRotate(String rotate) {
		Rotate = rotate;
	}

	public String getPosX() {
		return PosX;
	}

	public void setPosX(String posX) {
		PosX = posX;
	}

	public String getPosY() {
		return PosY;
	}

	public void setPosY(String posY) {
		PosY = posY;
	}

	public String getScaleX() {
		return ScaleX;
	}

	public void setScaleX(String scaleX) {
		ScaleX = scaleX;
	}

	public String getScaleY() {
		return ScaleY;
	}

	public void setScaleY(String scaleY) {
		ScaleY = scaleY;
	}

	public String getVisible() {
		return Visible;
	}

	public void setVisible(String visible) {
		Visible = visible;
	}

	public String getExpct() {
		return Expct;
	}

	public void setExpct(String expct) {
		Expct = expct;
	}

	public Map<String, String> getExpMap() {
		return ExpMap;
	}

	public void setExpMap(Map<String, String> expMap) {
		ExpMap = expMap;
	}

	public void init() {

		mPosX = Float.valueOf(JsUtil.getInstance().runCmd(PosX).toString());
		mPosY = Float.valueOf(JsUtil.getInstance().runCmd(PosY).toString());
		mScaleX = Float.valueOf(JsUtil.getInstance().runCmd(ScaleX).toString());
		mScaleY = Float.valueOf(JsUtil.getInstance().runCmd(ScaleY).toString());
		mAnchor = Constance.getAnchor(Anchor);
		mRotate = Double.valueOf(JsUtil.getInstance().runCmd(Rotate).toString());
		mVisible = Boolean.parseBoolean(Visible);

		if (Childs != null) {
			for (CCNode node : Childs) {
				node.init();
			}
		}
		super.init();
	}

	public float getLocPosX() {
		float pos = mPosX + CenterEditorView.getInstance().getBorderWidth();
		CCNode parentNode = ProjectInfoAdpter.getInstance().getParentNode(this);
		while (parentNode != null) {
			pos += parentNode.mPosX;
			parentNode = ProjectInfoAdpter.getInstance().getParentNode(parentNode);
		}
		return pos;
	}

	public float getLocPosY() {
		float pos = mPosY + CenterEditorView.getInstance().getBorderHeight();
		CCNode parentNode = ProjectInfoAdpter.getInstance().getParentNode(this);
		while (parentNode != null) {
			pos += parentNode.mPosY;
			parentNode = ProjectInfoAdpter.getInstance().getParentNode(parentNode);
		}
		return pos;
	}

	public void addChild(CCNode child) {
		if (child == null)
			return;
		if (Childs == null) {
			Childs = new ArrayList<CCNode>();
		}
		Childs.add(child);
	}

	public boolean RemoveChild(CCNode child) {
		if (child == null || Childs == null)
			return false;
		if (Childs.contains(child)) {
			return Childs.remove(child);
		}
		return false;
	}

	public List<CCNode> getChilds() {
		if (Childs == null) {
			Childs = new ArrayList<CCNode>();
		}
		return Childs;
	}

	public void setChilds(List<CCNode> childs) {
		Childs = childs;
	}

	public void update(float time) {
		if (Childs != null) {
			for (CCNode node : Childs) {
				node.update(time);
			}
		}
	}

	public void expAddMember(String member) {
		if (Expct == null) {
			Expct = JsUtil.getInstance().getJsString(Constance.CCEXP_JS_PATH);
		}

		if (ExpMap == null) {
			ExpMap = new HashMap<String, String>();
		}

		if (member == null || Expct == null)
			return;
		String replaceStr = null;
		String nameStr = null;
		if (member.contains("{") && member.contains("}")) {
			replaceStr = "//mothed-member-line";
			nameStr = FormulaUtil.getMothedName(member);
		} else {
			replaceStr = "//param-member-line";
			nameStr = FormulaUtil.getParamName(member);
		}

		if (nameStr != null) {
			ExpMap.put(nameStr, member);
		}
		Expct = Expct.replaceAll(replaceStr, member + "\n" + replaceStr);
	}

	public void expEditMember(String name, String member) {
		if (name == null || Expct == null || ExpMap == null)
			return;
		String oldMember = null;
		if (ExpMap.containsKey(name)) {
			oldMember = ExpMap.get(name);
			Expct.replaceAll(oldMember, member);
			ExpMap.put(name, member);
		}
	}

	public void expRemoveMember(String name) {
		if (Expct == null) {
			Expct = JsUtil.getInstance().getJsString(Constance.CCEXP_JS_PATH);
		}

		if (ExpMap == null) {
			ExpMap = new HashMap<String, String>();
		}

		if (name == null || Expct == null || ExpMap == null)
			return;
		String member = ExpMap.get(name);
		if (member != null) {
			ExpMap.remove(name);
			Expct.replaceAll(member, "");
		}
	}

	public void draw(Graphics g) {
		if (Childs != null && mVisible) {
			for (CCNode node : Childs) {
				node.draw(g);
			}
		}
	}
}
